# -*- coding: utf-8 -*-
# @Time    : 2024/12/9  15:49
# @Author  : Nabby
# @FileName: jasco_declaration_information_impl.py
# @Desc    : 申报信息
from app import db
from infrastructure.po.mode.jasco_declaration_information import (
    JascoDeclarationInformation,
)
from infrastructure.po.impl.jasco_loading_order_impl import JascoLoadingOrderImpl
from sqlalchemy import and_, or_, desc, asc


class JascoDeclarationInformationImpl(object):

    @classmethod
    def get_declaration_information_list(cls, order_id, page=1, page_size=999):
        query = db.session.query(JascoDeclarationInformation).filter(
            JascoDeclarationInformation.order_id == order_id
        )
        return (
            query.order_by(JascoDeclarationInformation.create_time.desc()).paginate(
                page=page, per_page=page_size
            ),
            query.count(),
        )

    @classmethod
    def add_declaration_information(
        cls,
        order_id,
        total_products,
        declared_value,
        name_quantity,
        english_name,
        chinese_name,
        customs_code,
        products_num,
        chinese_material,
        english_material,
        chinese_use,
        english_use,
        brand,
        model,
        online_link,
        product_picture,
        unit,
        price,
        currency,
        total_price,
        net_weight,
        source_area,
        final_destination,
    ):

        jasco_declaration_information = JascoDeclarationInformation(
            total_products=total_products,
            declared_value=declared_value,
            name_quantity=name_quantity,
            english_name=english_name,
            chinese_name=chinese_name,
            customs_code=customs_code,
            products_num=products_num,
            chinese_material=chinese_material,
            english_material=english_material,
            chinese_use=chinese_use,
            english_use=english_use,
            brand=brand,
            model=model,
            online_link=online_link,
            product_picture=product_picture,
            unit=unit,
            price=price,
            currency=currency,
            total_price=total_price,
            net_weight=net_weight,
            source_area=source_area,
            final_destination=final_destination,
            order_id=order_id,
        )
        print(jasco_declaration_information)
        db.session.add(jasco_declaration_information)
        db.session.flush()
        db.session.commit()
        return True

    @classmethod
    def edit_declaration_information(
        cls,
        id,
        order_id,
        total_products,
        declared_value,
        name_quantity,
        english_name,
        chinese_name,
        customs_code,
        products_num,
        chinese_material,
        english_material,
        chinese_use,
        english_use,
        brand,
        model,
        online_link,
        product_picture,
        unit,
        price,
        currency,
        total_price,
        net_weight,
        source_area,
        final_destination,
    ):
        query = db.session.query(JascoDeclarationInformation).filter(
            and_(
                JascoDeclarationInformation.id == id,
                JascoDeclarationInformation.order_id == order_id,
            )
        )
        if query.count() == 0:
            return False
        else:
            query.update(
                {
                    "total_products": total_products,
                    "declared_value": declared_value,
                    "name_quantity": name_quantity,
                    "english_name": english_name,
                    "chinese_name": chinese_name,
                    "customs_code": customs_code,
                    "products_num": products_num,
                    "chinese_material": chinese_material,
                    "english_material": english_material,
                    "chinese_use": chinese_use,
                    "english_use": english_use,
                    "brand": brand,
                    "model": model,
                    "online_link": online_link,
                    "product_picture": product_picture,
                    "unit": unit,
                    "price": price,
                    "currency": currency,
                    "total_price": total_price,
                    "net_weight": net_weight,
                    "source_area": source_area,
                    "final_destination": final_destination,
                }
            )
            db.session.commit()
            return True

    @classmethod
    def delete_declaration_information(cls, id, order_id):
        query = db.session.query(JascoDeclarationInformation).filter(
            and_(
                JascoDeclarationInformation.id == id,
                JascoDeclarationInformation.order_id == order_id,
            )
        )
        if query.count() > 0:
            loading_order_list, total  = JascoLoadingOrderImpl.get_loading_order_count_by_declaration_information_id(
                id
            )
            if total > 0:
                return False
            query.delete()
            db.session.commit()
            return True

